package panel;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import file.IOStreamStudent;
import model.Student;

public class ScorequduanPanel extends JFrame implements ActionListener{
	
	public static ScorequduanPanel instance=new ScorequduanPanel();
	Object Data[][]=new Object[5][3];
	String column[]=new String[]{"成绩区段","人数","百分比"};
	JPanel jPanel=new JPanel();
	public ScorequduanPanel(){	
		this.setTitle("成绩区段");
		this.setSize(700, 300);
		Data[0][0]="90-100";
		Data[1][0]="80-89";
		Data[2][0]="70-79";
		Data[3][0]="60-69";
		Data[4][0]="0-59";
		ArrayList<Student> list=new ArrayList<>();
		list=IOStreamStudent.readFromFile();
		int j = 0,k=0,l=0,m=0,n=0;
		for(int i=0;i<list.size();i++){
			if(list.get(i).getCourse().equals(ScorePage.instance.box.getSelectedItem().toString())&&
        			list.get(i).getTeacher().equals(ScorePage.instance.box2.getSelectedItem().toString())&&
        			list.get(i).getStu_class().equals(ScorePage.instance.box3.getSelectedItem().toString())){
			if(Integer.parseInt(list.get(i).getZongJi())>=90&&Integer.parseInt(list.get(i).getZongJi())<=100)
				j++;
			if(Integer.parseInt(list.get(i).getZongJi())>=80&&Integer.parseInt(list.get(i).getZongJi())<=89)
				k++;
			if(Integer.parseInt(list.get(i).getZongJi())>=70&&Integer.parseInt(list.get(i).getZongJi())<=79)
				l++;
			if(Integer.parseInt(list.get(i).getZongJi())>=60&&Integer.parseInt(list.get(i).getZongJi())<=69)
				m++;
			if(Integer.parseInt(list.get(i).getZongJi())>=0&&Integer.parseInt(list.get(i).getZongJi())<=59)
				n++;
			}
		}
		int count=j+k+l+m+n;
		if(count!=0){
			Data[0][1]=j;Data[0][2]=100*j/count+"%";
			Data[1][1]=k;Data[1][2]=100*k/count+"%";
			Data[2][1]=l;Data[2][2]=100*l/count+"%";
			Data[3][1]=m;Data[3][2]=100*m/count+"%";
			Data[4][1]=n;Data[4][2]=100*n/count+"%";
		}
		
		JTable table=new JTable(Data,column);
		JScrollPane pane=new JScrollPane(table);
		this.add(pane);
		setVisible(true);
		this.setLocationRelativeTo(null);//位于屏幕中央
		this.setResizable(false);//设置此窗体是否可由用户调整大小
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置默认关闭操作
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JButton button=(JButton) e.getSource();
		if(button==new ScoreTable().quduan){
			new ScorequduanPanel();
		}
	}
}
